﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class CommunityOrder : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["CommunityUser"] == null)
        {
            Response.Redirect("Community.aspx");
        }
        if (!this.IsPostBack)
        {
            this.SqlDataSource1.SelectCommand = "SELECT top 1 [RemainMoney],[Cent] FROM [GowllUsers] WHERE [ID]=" + Session["CommunityUserID"];
            this.Repeater1.DataBind();
            this.SqlDataSource2.SelectCommand = "SELECT [GowllCommunityOrders].[ID], [GowllCommunitys].[Title], [GowllCommunitys].[Price],[GowllCommunityOrders].[StartDate] FROM [GowllCommunitys],[GowllCommunityOrders] WHERE [GowllCommunityOrders].[Payed]=0 and [GowllCommunityOrders].[CommunityID]=[GowllCommunitys].[ID] and [GowllCommunityOrders].[UserID]=" + Session["CommunityUserID"] + " ORDER BY [GowllCommunityOrders].[StartDate] DESC ";
            this.GridView1.DataBind();
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        SqlConnection MySqlConnection = new SqlConnection();
        MySqlConnection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["GoWLLConnectionString"].ConnectionString;
        MySqlConnection.Open();
        SqlCommand MySqlCommand = new SqlCommand();
        MySqlCommand.CommandType = CommandType.Text;
        MySqlCommand.CommandText = "DELETE FROM [GowllCommunityOrders] WHERE [ID]=" + ((LinkButton)sender).CommandName;
        MySqlCommand.Connection = MySqlConnection;
        MySqlCommand.ExecuteNonQuery();
        MySqlConnection.Close();
        MySqlCommand.Dispose();
        MySqlConnection.Dispose();
        this.SqlDataSource2.SelectCommand = "SELECT [GowllCommunityOrders].[ID], [GowllCommunitys].[Title], [GowllCommunitys].[Price],[GowllCommunityOrders].[StartDate] FROM [GowllCommunitys],[GowllCommunityOrders] WHERE [GowllCommunityOrders].[Payed]=0 and [GowllCommunityOrders].[CommunityID]=[GowllCommunitys].[ID] and [GowllCommunityOrders].[UserID]=" + Session["CommunityUserID"] + " ORDER BY [GowllCommunityOrders].[StartDate] DESC";
        this.GridView1.DataBind();
    }
    protected void LinkButton1_Init(object sender, EventArgs e)
    {
        ((LinkButton)sender).Attributes["onclick"] = "return(confirm('确定要取消订单吗？'))";
    }
    protected void LinkButton2_Init(object sender, EventArgs e)
    {
        ((LinkButton)sender).Attributes["onclick"] = "return(confirm('确定要支付该订单吗？点击“确定”将从您的社区余额中扣出费用。'))";
    }
    protected void LinkButton3_Init(object sender, EventArgs e)
    {
        ((LinkButton)sender).Attributes["onclick"] = "return(confirm('确定要用积分支付该订单吗？点击“确定”将从您的社区积分中扣出费用。'))";
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        int UserID = (int)Session["CommunityUserID"];
        int OrderID = int.Parse(((LinkButton)sender).CommandName);

        SqlConnection MySqlConnection = new SqlConnection();
        MySqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["GoWLLConnectionString"].ConnectionString;
        MySqlConnection.Open();

        SqlCommand MySqlCommand = new SqlCommand();
        MySqlCommand.CommandType = CommandType.StoredProcedure;
        MySqlCommand.CommandText = "PayCommunityOrder";
        MySqlCommand.Connection = MySqlConnection;
        MySqlCommand.Parameters.Add(new SqlParameter("@UserID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, UserID));
        MySqlCommand.Parameters.Add(new SqlParameter("@OrderID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, OrderID));
        MySqlCommand.Parameters.Add(new SqlParameter("@Result", SqlDbType.Int, 4, ParameterDirection.Output, true, 0, 0, "", DataRowVersion.Proposed, null));
        Application.Lock();
        MySqlCommand.ExecuteNonQuery();
        Application.UnLock();
        if ((int)MySqlCommand.Parameters[2].Value==1)
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language=javascript>alert('支付失败，该社区已被抢购。');</script>");
        }
        else if ((int)MySqlCommand.Parameters[2].Value == 2)
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language=javascript>alert('支付失败，您的余额不足。');</script>");
        }
        else
        {
            this.SqlDataSource1.SelectCommand = "SELECT top 1 [RemainMoney],[Cent] FROM [GowllUsers] WHERE [ID]=" + Session["CommunityUserID"];
            this.Repeater1.DataBind();
            this.SqlDataSource2.SelectCommand = "SELECT [GowllCommunityOrders].[ID], [GowllCommunitys].[Title], [GowllCommunitys].[Price],[GowllCommunityOrders].[StartDate] FROM [GowllCommunitys],[GowllCommunityOrders] WHERE [GowllCommunityOrders].[Payed]=0 and [GowllCommunityOrders].[CommunityID]=[GowllCommunitys].[ID] and [GowllCommunityOrders].[UserID]=" + Session["CommunityUserID"] + " ORDER BY [GowllCommunityOrders].[StartDate] DESC ";
            this.GridView1.DataBind();
            this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language=javascript>alert('支付成功。');</script>");
        }
        MySqlConnection.Close();
        MySqlCommand.Dispose();
        MySqlConnection.Dispose();
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        int UserID = (int)Session["CommunityUserID"];
        int OrderID = int.Parse(((LinkButton)sender).CommandName);

        SqlConnection MySqlConnection = new SqlConnection();
        MySqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["GoWLLConnectionString"].ConnectionString;
        MySqlConnection.Open();

        SqlCommand MySqlCommand = new SqlCommand();
        MySqlCommand.CommandType = CommandType.StoredProcedure;
        MySqlCommand.CommandText = "PayCommunityOrderByCent";
        MySqlCommand.Connection = MySqlConnection;
        MySqlCommand.Parameters.Add(new SqlParameter("@UserID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, UserID));
        MySqlCommand.Parameters.Add(new SqlParameter("@OrderID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, OrderID));
        MySqlCommand.Parameters.Add(new SqlParameter("@Result", SqlDbType.Int, 4, ParameterDirection.Output, true, 0, 0, "", DataRowVersion.Proposed, null));
        Application.Lock();
        MySqlCommand.ExecuteNonQuery();
        Application.UnLock();
        if ((int)MySqlCommand.Parameters[2].Value == 1)
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language=javascript>alert('支付失败，该社区已被抢购。');</script>");
        }
        else if ((int)MySqlCommand.Parameters[2].Value == 2)
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language=javascript>alert('支付失败，您的积分不足。');</script>");
        }
        else
        {
            this.SqlDataSource1.SelectCommand = "SELECT top 1 [RemainMoney],[Cent] FROM [GowllUsers] WHERE [ID]=" + Session["CommunityUserID"];
            this.Repeater1.DataBind();
            this.SqlDataSource2.SelectCommand = "SELECT [GowllCommunityOrders].[ID], [GowllCommunitys].[Title], [GowllCommunitys].[Price],[GowllCommunityOrders].[StartDate] FROM [GowllCommunitys],[GowllCommunityOrders] WHERE [GowllCommunityOrders].[Payed]=0 and [GowllCommunityOrders].[CommunityID]=[GowllCommunitys].[ID] and [GowllCommunityOrders].[UserID]=" + Session["CommunityUserID"] + " ORDER BY [GowllCommunityOrders].[StartDate] DESC ";
            this.GridView1.DataBind();
            this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language=javascript>alert('支付成功。');</script>");
        }
        MySqlConnection.Close();
        MySqlCommand.Dispose();
        MySqlConnection.Dispose();
    }
}
